//
//  JYTSchoolViewController.m
//  JYT_IOS
//
//  Created by nxa-tankeke on 16/12/21.
//  Copyright © 2016年 nxa-yuanguoxiang. All rights reserved.
//

#import "JYTSchoolViewController.h"
#import "JYTCityZoneView.h"
#import "JYTCity.h"
#import "JYTZone.h"
#import "JYTTestViewController.h"
#import "JYTPopupMenu.h"
#import "JYTSchoolTableViewCell.h"
#import <MJRefresh.h>
#import "JYTSchoolHandler.h"
#import "JYTSearchSchoolViewController.h"
#import "SchoolInfoViewController.h"


#define TITLES @[@"星级", @"先学后付", @"价格", @"人气",@"接送"]
#define ICONS  @[@"motify",@"motify",@"motify",@"motify",@"motify"]

@interface JYTSchoolViewController ()<JYTPopupMenuDelegate, UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)JYTCity *currentCity;
@property (nonatomic, strong)JYTZone *currentZone;
@property (nonatomic, strong)UIButton *btn;
@property (nonatomic, copy)NSString *currentFilter;
@property (nonatomic, strong)UIButton *slectBtn;
@property (nonatomic, strong)UITableView *schoolList;

@property (nonatomic, strong)NSMutableArray * schools;

@property (nonatomic, assign)NSInteger page;
@property (nonatomic, strong)NSDictionary * parms;
@end

@implementation JYTSchoolViewController

-(NSMutableArray *)schools
{
    if (_schools == nil) {
        _schools = [NSMutableArray array];
    }
    
    return _schools;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSDictionary *dic = @{@"lftBtnBgImg":@"ic_goback",
                          @"lftBtnTitle":@"no",
                          @"pageTitle":@"找驾校",
                          @"rhtBtnBgImg":@"ic_goback",
                          @"rhtBtnTitle":@"no"};
    
    [self setNavgationBarWithDic:dic];
  
    
    BOOL isUsedOffLine = [[JYTUserManager shared] isUsedOffLine];
    if (!isUsedOffLine) {
        
        User *user = [[JYTUserManager shared] getUser];
        _currentCity = [[JYTUserManager shared] getCityById:user.cityId];
        
    }else {
        _currentCity =[[[JYTUserManager shared] getCityArr] firstObject];
        
    }
    
    JYTZone * first = [[JYTZone alloc]init];
    first.zone_id = _currentCity.zone_id;
    first.zone_name = _currentCity.zone_name;
    first.parent_id = _currentCity.parent_id;
    first.service_url = _currentCity.service_url;
    first.weixin_url = _currentCity.weixin_url;
    _currentZone = first;
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:self action:@selector(slectZone) forControlEvents: UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor whiteColor];
    btn.frame = CGRectMake(0, 64, CURRENT_SCREEN_WIDTH / 2, 46);
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:16.0]];
    [btn setTitle:_currentCity.zone_name forState:UIControlStateNormal];
    [btn.layer setBorderWidth:1.0];
    self.btn = btn;
    [self.view addSubview:btn];
    
    UIButton *slectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    slectBtn.backgroundColor = [UIColor whiteColor];
    slectBtn.frame = CGRectMake(CURRENT_SCREEN_WIDTH / 2, 64, CURRENT_SCREEN_WIDTH / 2, 46);
    [slectBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [slectBtn.titleLabel setFont:[UIFont systemFontOfSize:16.0]];
    [slectBtn setTitle:@"星级" forState:UIControlStateNormal];
    [slectBtn.layer setBorderWidth:1.0];
    _currentFilter = @"0";
    [slectBtn addTarget:self action:@selector(slectFilter:) forControlEvents:UIControlEventTouchUpInside];
    self.slectBtn = slectBtn;
    [self.view addSubview:slectBtn];
    return;
    [self setupTableView];
    
    [self setupRefresh];
}


- (void)slectFilter:(UIButton *)sender
{
   [JYTPopupMenu showRelyOnView:sender titles:TITLES icons:ICONS menuWidth:150 delegate:self];

}

- (void)slectZone
{
    
        JYTCityZoneView *cz = [[JYTCityZoneView alloc]initWithFrame:CGRectMake(0, 64, CURRENT_SCREEN_WIDTH, 300)];
        [cz setSelect:^(NSInteger index, JYTZone *zone) {
            //MyLog(@"---------%@",zone.zone_name);
            _currentZone = zone;
            [self.btn setTitle:zone.zone_name forState:UIControlStateNormal];
            
            [self.schoolList.mj_header beginRefreshing];
            [self.schools removeAllObjects];
            [self.schoolList reloadData];
            [self loadNewSchools];
        }];
        [cz show];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)popupMenuDidSelectedAtIndex:(NSInteger)index popupMenu:(JYTPopupMenu *)popupMenu
{
    [self.slectBtn setTitle:TITLES[index] forState:UIControlStateNormal];
    _currentFilter = [NSString stringWithFormat:@"%zd", index];
    MyLog(@"===%@", _currentFilter);
    [self.schoolList.mj_header beginRefreshing];
    [self.schools removeAllObjects];
    [self.schoolList reloadData];
    [self loadNewSchools];
}


static NSString  *JYTSchoolCellId = @"school";
- (void)setupTableView
{
//    self.tableView.backgroundColor = [UIColor clearColor];
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([JYTSchoolTableViewCell class]) bundle:nil] forCellReuseIdentifier:JYTSchoolCellId];
    
    UITableView *schoolList = [[UITableView alloc]initWithFrame:CGRectMake(0, 110, CURRENT_SCREEN_WIDTH, CURRENT_SCREEN_HEIGHT - 110) style:UITableViewStylePlain];
    schoolList.dataSource = self;
    schoolList.delegate = self;
    schoolList.backgroundColor = [UIColor clearColor];
    schoolList.separatorStyle = UITableViewCellSeparatorStyleNone;
    [schoolList registerNib:[UINib nibWithNibName:NSStringFromClass([JYTSchoolTableViewCell class]) bundle:nil] forCellReuseIdentifier:JYTSchoolCellId];
    self.schoolList = schoolList;
    [self.view addSubview:schoolList];
}

- (void)setupRefresh
{
    self.schoolList.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreSchools)];
    
    self.schoolList.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewSchools)];
    
    self.schoolList.mj_header.automaticallyChangeAlpha = YES;
    
    [self.schoolList.mj_header beginRefreshing];
    
    
}
- (void)loadNewSchools
{
    [self.schoolList.mj_footer endRefreshing];
    
    NSMutableDictionary *parms = [NSMutableDictionary dictionary];
    
    [parms setObject:_currentZone.zone_id forKey:@"ZoneId"];
    [parms setObject:_currentFilter forKey:@"filter"];
    [parms setObject:@"0" forKey:@"PageNo"];
    [parms setObject:@"10" forKey:@"PageSize"];
    
    self.parms = parms;
    
    [[JYTSchoolHandler shared] getSchoolWithParam:parms WithHandler:^(NSArray *coaches, NSString *pageNo) {
        if (self.parms != parms) {
            return ;
        }
        
        if (coaches.count > 0) {
            
            [self.schools removeAllObjects];
            
            [self.schools addObjectsFromArray:coaches];
            
            [self.schoolList reloadData];
            
            [self.schoolList.mj_header endRefreshing];
            
            self.page = 0;
            
        } else {
            if (self.parms != parms) {
                return ;
            }
            
            [self.schoolList.mj_header endRefreshing];
        }
    }];
}


- (void)loadMoreSchools
{
    [self.schoolList.mj_header endRefreshing];
    
    NSInteger page = self.page + 1;
    NSString *pageStr = [NSString stringWithFormat:@"%ld", page];
    
    NSMutableDictionary *parms = [NSMutableDictionary dictionary];
    
    [parms setObject:_currentZone.zone_id forKey:@"ZoneId"];
    
    [parms setObject:_currentFilter forKey:@"filter"];
    [parms setObject:pageStr forKey:@"PageNo"];
    [parms setObject:@"10" forKey:@"PageSize"];
    self.parms = parms;
    
    [[JYTSchoolHandler shared] getSchoolWithParam:parms WithHandler:^(NSArray *coaches, NSString *pageNo) {
        if (self.parms != parms) {
            return ;
        }
        
        if (coaches.count > 0) {
            
            [self.schools addObjectsFromArray:coaches];
            
            [self.schoolList reloadData];
            
            [self.schoolList.mj_footer endRefreshing];
            
            self.page = page;
        } else {
            if (self.parms != parms) {
                return ;
            }
            
            [self.schoolList.mj_footer endRefreshing];
        }
        
    }];
    
}


#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    self.schoolList.mj_footer.hidden = (self.schools.count == 0);
    return self.schools.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    JYTSchoolTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:JYTSchoolCellId];
    
    
    cell.school = self.schools[indexPath.row];

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SchoolInfoViewController *sInfo = [[SchoolInfoViewController alloc]init];
    sInfo.school = self.schools[indexPath.row];
    [self pushViewController:sInfo];
}
- (void) baseGoToNextViewController
{
    return;
    JYTSearchSchoolViewController *sear = [[JYTSearchSchoolViewController alloc]init];
    [self pushViewController:sear];
}
 
@end
